package com.junchenrobot.mes.dao;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.junchenrobot.mes.domain.Device;
import com.junchenrobot.mes.mapper.DeviceMapper;
import io.netty.util.internal.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.List;

/**
 * 设备Dao
 *
 * @author SeaWave
 * @date 2022/10/25
 */
@Slf4j
@Component
public class DeviceDao {
    
    
    private final DeviceMapper deviceMapper;
    
    @Autowired
    public DeviceDao(DeviceMapper deviceMapper) {
        this.deviceMapper = deviceMapper;
    }
    
    
    /**
     * 查询设备
     *
     * @param deviceName 设备名称
     * @return {@link Device}
     */
    public Device getDevice(String deviceName) {
        return new LambdaQueryChainWrapper<>(deviceMapper).eq(Device::getDeviceCode, deviceName).last("LIMIT 1").one();
    }
    
    /**
     * 设备列表
     *
     * @return {@link List}<{@link Device}>
     */
    public List<Device> listDevices() {
        return new LambdaQueryChainWrapper<>(deviceMapper)
                .orderByAsc(Device::getDeviceName)
                .list();
    }

    /**
     * 修改设备状态
     * @param deviceName 设备名称
     * @param status 设备状态
     */
    public void updateDevice(String deviceName, Integer status) {
        Device device = new Device();
        device.setDeviceStatus(status);
        UpdateWrapper<Device> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("device_name", deviceName);
        log.info("将{}号车的状态改为{}",deviceName,status);
        deviceMapper.update(device, updateWrapper);
    }

    /**
     * 修改设备电量
     * @param deviceName 设备名称
     * @param bigDecimal 设备电量
     */
    public void updateDeviceBattery(String deviceName, String bigDecimal) {
        Device device = new Device();
        device.setDeviceBattery(bigDecimal);
        UpdateWrapper<Device> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("device_name", deviceName);
        deviceMapper.update(device, updateWrapper);
    }


    public void updateBatch(List<Device> deviceList) {
        UpdateWrapper<Device> updateWrapper = new UpdateWrapper<>();
        for (Device device : deviceList) {
            updateWrapper.eq("device_name", device.getDeviceName());
            deviceMapper.update(device, updateWrapper);
            updateWrapper.clear();
        }
    }

    /**
     * 修改单根杆子状态
     * @param deviceName 设备名称
     * @param pole 杆子编号
     * @param status 杆子状态
     */
    public void updateDevicePoleStatus(String deviceName, Integer pole, Integer status) {
        Device device = new Device();
        if (pole == 1) {
            device.setDeviceLeftPole(status);
        }
        if (pole == 2) {
            device.setDeviceRightPole(status);
        }
        UpdateWrapper<Device> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("device_name", deviceName);
        int update = deviceMapper.update(device, updateWrapper);
        log.info("将{}号车的{}杆状态改为{},修改状态{}",deviceName,pole,status,update);
    }

    
    
    /**
     * 更新设备
     *
     * @param device 设备
     */
    public void updateDevice(Device device) {
        deviceMapper.updateById(device);
    }
    
    
    /**
     * 满杆状态
     *
     * @param deviceName 设备名称
     * @return boolean
     */
    public boolean isFullState(String deviceName) {
        return new LambdaQueryChainWrapper<>(deviceMapper).eq(Device::getDeviceName, deviceName)
                                                          .eq(Device::getDeviceLeftPole, 1)
                                                          .eq(Device::getDeviceRightPole, 1)
                                                          .last("LIMIT 1")
                                                          .count() == 1;
    }
    
}
